package com.qmx.qmxapp.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.token.TokenManager;
import com.tencent.ilivesdk.core.ILiveLoginManager;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.Realm;

/**
 * j加入会议
 */
public class JoinMeetingActivity extends BaseActivity {

    @BindView(R.id.ed_join_meeting_tips)
    EditText ed_join_meeting_tips;
    @BindView(R.id.btn_join_meeting)
    Button btn_join_meeting;
    @BindView(R.id.tv_join_meeting_use_phone)
    TextView tv_join_meeting_use_phone;
    @BindView(R.id.tv_change_keyboard_type)
    TextView tv_change_keyboard_type;
    @BindView(R.id.cb_close_voice)
    CheckBox cb_close_voice;
    @BindView(R.id.cb_close_camera)
    CheckBox cb_close_camera;

    private String roomId = "";
    private Gson mGson = new Gson();
    private Intent intent;
    //    private String __TOKEN__ = "";
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_meeting);
        ButterKnife.bind(this);
        ed_join_meeting_tips.setText(sharedPreferences.getInt(AppConstant.ROOM_ID, 0) + "");
        realm = Realm.getDefaultInstance();

    }

    @OnClick({R.id.btn_join_meeting, R.id.tv_join_meeting_use_phone, R.id.tv_change_keyboard_type})
    public void onclick(View v) {
        switch (v.getId()) {
            case R.id.btn_join_meeting:
                token(realm);
                roomId = ed_join_meeting_tips.getText().toString();
                if (roomId.isEmpty() || __TOKEN__.isEmpty()) {
                    showToast("请填写会议号");
                    return;
                }
                joinMeeting();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.isClosed();
        finish();
    }

    public void joinMeeting() {
        OkGo.<String>post(AppConstant.Url + ComParamContact.Launch.PATH)
                .params(ComParamContact.Launch.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .params(ComParamContact.Launch.__TOKEN__, __TOKEN__)
                .params(ComParamContact.Launch.ROOM_ID, roomId)
                .params(ComParamContact.Launch.CLIENT_ID, sharedPreferences.getString(AppConstant.CLIENT_ID, ""))
                .params(ComParamContact.Launch.PASSWORD, "")
                .execute(new JsonCallback<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //                        String str = mGson.toJson(response.body());
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            String json = jsonObject.optString("code");
                            String msg = jsonObject.optString("msg");
                            //                        100：平台无此会议号 101：请输入密码 102：密码错误 103：方数不足 104：加入成功 104：短线重连
                            if (json.equals("1")) {
                                Intent intent = new Intent(JoinMeetingActivity.this, RoomActivity.class);
                                //                                Log.e("roomId==", roomId);
                                intent.putExtra("roomId", roomId);
                                startActivity(intent);
                            } else if (json.equals("0")) {
                                showToast(msg);
                            } else if (json.equals("102")) {
                                showInputDialog();
                                showToast(msg);
                            } else if (json.equals("102")) {
                                showToast(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        Log.i("joinMeeting", response.body() + "");


                    }

                    @Override
                    public void onError(Response<String> response) {
                        Log.i("onError", response.getException().getMessage());
                        showToast(response.getException().getMessage());
                    }
                });
    }

    private AlertDialog inputDlg;

    private void showInputDialog() {
        if (null != inputDlg && inputDlg.isShowing()) {
            inputDlg.dismiss();
            inputDlg = null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.full_dlg);
        builder.setTitle(R.string.str_problem_other);

        final EditText input = new EditText(this);
        input.setSingleLine(false);
        input.setTextColor(Color.BLACK);
        builder.setView(input);

        builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        inputDlg = builder.create();
        inputDlg.setCanceledOnTouchOutside(true);
        inputDlg.show();
    }

}
